package Server.DataBase;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
 * 
 * /**
 * implement all SemesterQuery  handler
 * */
 public class SemesterQuery extends Query{
/**
 * constructor semester
 */
	public SemesterQuery() {
		super();
	}
	/**
	 * 
	 * @param idSemester
	 * @return Semester
	 * @throws SQLException
	 */
	public Semester getSemester(int idSemester) throws SQLException{
		setQuery("SELECT * FROM semester WHERE idsemester='"+idSemester+"'");
		ResultSet rs = execQuery();
		if (rs.next())
			return new Semester(idSemester, rs.getInt(2), rs.getInt(3));
		else return null;
		
	}
	/**
	 * create new semester in table
	 * @throws SQLException
	 */
	public void newSemester() throws SQLException{
		int year, session, current;
		Info info = new Info();
		current = info.getCurrentSemesterId();
		setQuery("SELECT * FROM semester WHERE idsemester='"+current+"'");
		ResultSet rs = execQuery();
		rs.next();
		year = rs.getInt(2);
		if ((session=rs.getInt(3))==2){
			year++;
			session--;
		}
		else session++;
		setQuery("INSERT INTO semester(idsemester, year, session) VALUES(?,?,?)");
		PreparedStatement query = getPS();
		query.setInt(1, current+1);
		query.setInt(2, year);
		query.setInt(3, session);
		query.executeUpdate();
		info.incSemesterId();
	}
	/**
	 * 
	 * @return semester table
	 * @throws SQLException
	 */
	public ResultSet getTable() throws SQLException{
		setQuery("SELECT * FROM semester");
		return execQuery();
	}

}
